package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.entity.wms.WarehouseAreaEntity;
import com.group.entity.wms.WarehouseEntity;
import com.group.exception.IndustryException;
import com.group.mapper.WarehouseMapper;
import com.group.service.WareHouseAreaService;
import com.group.service.WarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, WarehouseEntity> implements WarehouseService {

    @Resource
    private WareHouseAreaService wareHouseAreaService;
    /**
     * 查询所有仓库信息
     * @return
     */
    @Override
    public List<WarehouseEntity> findAll() {

        return this.baseMapper.selectList(null);
    }

    /**
     * 添加一个仓库
     * @param warehouseEntity
     * @return
     */
    @Override
    public Integer add(WarehouseEntity warehouseEntity) {
        warehouseEntity.setCreateUser(1024L);
        warehouseEntity.setCreateTime(new Date());
        boolean save = this.save(warehouseEntity);
        return save?1:0;
    }

    @Override
    public Integer update(WarehouseEntity warehouseEntity) {

        warehouseEntity.setLastUpdateTime(new Date());
        warehouseEntity.setLastUpdateUser(1024L);
        int i = this.baseMapper.updateById(warehouseEntity);
        return i;
    }

    @Override
    public Integer delete(Long id) {
        //当前仓库是否存在库区信息
        List<WarehouseAreaEntity> warehouseAreaEntityList = this.wareHouseAreaService.getBaseMapper().selectList(new QueryWrapper<WarehouseAreaEntity>().eq("warehouse_id", id));

        if (warehouseAreaEntityList!=null&&warehouseAreaEntityList.size()>=1) throw new IndustryException("仓库下含有库区信息无法删除");


        return this.baseMapper.deleteById(id);
    }

    @Override
    public List<WarehouseEntity> findByNameLike(String keyWorld) {

        List<WarehouseEntity> warehouse_name = this.baseMapper.selectList(new QueryWrapper<WarehouseEntity>().like("warehouse_name", keyWorld));

        return warehouse_name;
    }
}
